﻿/*
 * User: artem
 * Date: 27.04.2011
 * Time: 18:44
 */
using System;
using System.Collections.Specialized;
using System.IO;
using System.Windows.Forms;

namespace Client.GUI
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		Logic.Controller controller = null;
		
		public MainForm()
		{
			InitializeComponent();
            try
            { controller = Logic.Controller.Instance; }
            catch (Exception exp)
            { ErrorShower.Show(exp); }
		}
		
		private void MonthCalendarEventDateDateSelected(object sender, 
		                                                DateRangeEventArgs e)
		{
			try
			{
                UpdateEventList();
			}
			catch (Exception exp)
            { ErrorShower.Show(exp); }
		}

        private void UpdateEventList()
        {
            dataGridViewEvents.Rows.Clear();

            var event_list = controller.GetEvents(monthCalendarEventDate.SelectionStart,
                                                  monthCalendarEventDate.SelectionEnd);

            foreach (var it in event_list)
                dataGridViewEvents.Rows.Add(it);
        }
		
		private void DataGridViewEventsSelectionChanged(object sender, 
		                                                EventArgs e)
		{
			btnEdit.Enabled = 
				btnRemove.Enabled =
                    deleteToolStripMenuItem.Enabled = 
                        editToolStripMenuItem.Enabled =
				            dataGridViewEvents.SelectedRows.Count != 0;
		}
		
		private void AddEventHandler(object sender, EventArgs e)
		{
            try
            {
                AddEditForm frm = new AddEditForm();
                frm.ShowDialog();

                UpdateEventList();
            }
            catch (Exception exp)
            { ErrorShower.Show(exp); }
		}
		
		private void EditEventHandler(object sender, EventArgs e)
		{
            try
            {
                AddEditForm frm = new AddEditForm(GetSelectedEventID());
                frm.ShowDialog();

                UpdateEventList();
            }
            catch (Exception exp)
            { ErrorShower.Show(exp); }
		}

        private void RemoveEventHandler(object sender, EventArgs e)
        {
            try
            {
            	DialogResult result = MessageBox.Show("Delete Event?",
            		                                  "Delete?",
            		                                  MessageBoxButtons.YesNo,
            		                                 MessageBoxIcon.Warning);
            	if (result == DialogResult.No)
            		return;
            	
                int deleted_id = GetSelectedEventID();
                controller.DeleteEvent(deleted_id);

                UpdateEventList();
            }
            catch (Exception exp)
            { ErrorShower.Show(exp); }
        }

        private int GetSelectedEventID()
        {
            const int first = 0;
            const int id_index = 0;

            return Int32.Parse(dataGridViewEvents.SelectedRows[first].Cells[id_index].Value.ToString());
        }

        private void showAboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Written by:\nAkulyakov A. A.\n" +
        	                "Kulachenko D. P.\n" +
        	                "Aldoshkin D. N.\n" +
        	                "SFU IKIT PU07-05\n2010-2011",
        	                "About",
        	                MessageBoxButtons.OK,
        	                MessageBoxIcon.Information);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnMake_Click(object sender, EventArgs e)
        {
            try
            {
                richTextBoxSchedule.Text = 
                    controller.BuildSchedule(monthCalendarEventDate.SelectionStart,
                                             monthCalendarEventDate.SelectionEnd);
            }
            catch (Exception exp)
            { ErrorShower.Show(exp); }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            const string default_save_extention = @"*.schedule";
            try
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.SupportMultiDottedExtensions = false;
                dlg.DefaultExt = default_save_extention;
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    controller.SaveSchedule(richTextBoxSchedule.Text, 
                                            dlg.OpenFile());
            }
            catch (Exception exp)
            { ErrorShower.Show(exp); }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            { controller.PrintSchedule(richTextBoxSchedule.Text); }
            catch (Exception exp)
            { ErrorShower.Show(exp); }
        }

        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // some code
        }
	}
}
